package com.link.base.base.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.link.core.cllog.LCLogger;
import com.link.core.basic.model.BasicModel;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.security.Principal;

//import org.springframework.messaging.simp.SimpMessagingTemplate;
/**
 * Copyright  hande Company. All Rights Reserved.
 *
 * @author  unknwon
 * @date:   2018/11/9 14:33
 * @version 1.0
 */

@Controller
@RequestMapping("/websocket")
public class SessionMonitorController {

    //@Resource
    //private SimpMessagingTemplate messagingTemplate;

    /**
     * 已经配置了前缀为/sessionMonitor; so=》/sessionMonitor/websocketTest
     * @param user
     */
    @MessageMapping("/websocketTest")
    public void sessionExpired(String user) {
        String username = "8527";
        System.err.println("WebSocket Test");
        System.out.println(user);
        //messagingTemplate.convertAndSend("/sessionMonitor/sessionExpired/" + username, "推送消息测试1");
    }

    @RequestMapping("/websocketTest2")
    public void sessionExpired2(String username, Principal principal, HttpSession session) {
        if (username == null || "".equals(username)) {
            username = "8527";
        }
        System.err.println("WebSocket Test2ss");
        System.out.println(principal);
        //messagingTemplate.convertAndSend("/sessionMonitor/sessionExpired/" + username, "推送消息测试2");
    }

    @RequestMapping("/test3")
    public void sessionExpired2(Principal principal, HttpSession session, HttpServletRequest request) {
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            BasicModel model = objectMapper.readValue(request.getInputStream(), BasicModel.class);
            System.out.println(model.getAttr1());
            System.out.println(model.getAttr2());
        } catch (IOException e) {
            LCLogger.withException(e);
        }
    }
}
